
var timerButton = document.getElementById('timer')
var messageButton = document.getElementById('message')


var counter = 1


/*
  
  class Promise<T>(cb)



  class Promise<T>(cb: (complete, fail) => void)
  
                     complete: (value: T) => void
                     fail: (error: Error) => void


  class Promise<T>(cb: (complete: (value: T) => void, fail: (error: Error) => void) => void)

  
  

  松散参数类型


  class Promise<T>(cb: (complete, fail) => void) {
    then<V>(cb: (value: T), error: Error): Promise<V>
    catch(error: Error)
  }


  class Promise(cb) {
    then(cb, error)
    catch(cb)
  }
*/

function changePromise(): Promise<void> { 
  return new Promise<void>(function (complete, fail) {
    setTimeout(function () {
      timerButton.innerHTML = String(counter)
      counter++
      complete()
    }, 1000)
  })
}

function changeYield() {

  function * eachChange(): Iterator<Promise<void>> {
    yield changePromise()
    yield changePromise()
    yield changePromise()
    yield changePromise()
    yield changePromise()
    yield changePromise()
    messageButton.innerHTML = 'Promise Yield finished'
  }

  var it = eachChange()

  function cb() {
    var result = it.next()
    if (!result.done) {
      var promise = result.value
      promise.then(function () {
        cb()
      })
    }
  }

  cb()
}

changeYield()

// changePromise()
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     return changePromise()
//   })
//   .then(() => {
//     messageButton.innerHTML = 'Promise2 finished'
//   })